package com.jifeng.api.crud.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.jifeng.api.crud.dao.VwTerminalUserDao;
import com.jifeng.api.crud.entity.VwTerminalUserEntity;
import com.jifeng.api.crud.entity.VwTerminalUserEntity;
import com.jifeng.api.crud.service.VwTerminalUserService;
import com.jifeng.api.utils.HttpUtils;
import com.jifeng.api.utils.PageUtils;
import com.jifeng.api.utils.Query;
import com.jifeng.api.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Service("vwTerminalUserService")
@Slf4j
public class VwTerminalUserServiceImpl extends ServiceImpl<VwTerminalUserDao, VwTerminalUserEntity> implements VwTerminalUserService {
    public HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }

    public String getRequestUrl() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest().getRequestURI();
    }

    @Override
    public R queryPage(Map<String, Object> params) {
        try {
            QueryWrapper<VwTerminalUserEntity> queryWrapper = new QueryWrapper<>();

            /**
             * 解析token获取登录用户信息
             */
            Map<String, Object> userInfo = HttpUtils.getUserIdByToken(getRequest());
            if (userInfo == null) {
                log.warn("path：{}，errMsg：{}，cause：{}", getRequestUrl(), "获取token信息失败", "未知异常");
                return R.error("获取token信息失败");
            }

            /**
             * 判断登录用户类型
             */
            String userType = Convert.toStr(userInfo.get("type"));
            //如果为户主只能查询自己的机柜
            if (userType.equals("2")) {
                String userId = Convert.toStr(userInfo.get("id"));
                queryWrapper.eq("user_id", userId);
            }

            String userName = Convert.toStr(params.get("userName"));
            String ID = Convert.toStr(params.get("ID"));
            String location = Convert.toStr(params.get("location"));
            String createTime = Convert.toStr(params.get("createTime"));


            /**
             * 判断登录用户类型
             */
            if (!StrUtil.isEmpty(userName)) {
                if (userType.equals("2")) {
                    String name = Convert.toStr(userInfo.get("name"));
                    queryWrapper.eq("name", name);
                } else if (userType.equals("1")) {
                    queryWrapper.like("name", userName);
                }
            }

            if (!StrUtil.isEmpty(ID)) {
                queryWrapper.eq("IOT", ID);
            }

            if (!StrUtil.isEmpty(location)) {
                queryWrapper.eq("location", location);
            }

            if (!StrUtil.isEmpty(createTime)) {
                queryWrapper.like("create_time", createTime);
            }

            IPage<VwTerminalUserEntity> page = this.page(
                    new Query<VwTerminalUserEntity>().getPage(params),
                    queryWrapper
            );

            return R.ok().put("page", new PageUtils(page));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("path：{}，errMsg：{}", getRequestUrl(), e.getMessage());
            return null;
        }
    }

}